package net.lcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.UserMapper;
import net.lcheng.model.User;
import net.lcheng.model.UserExample;
import net.lcheng.service.UserService;
import net.lcheng.vo.WxAccessTokenModel;
import net.lcheng.vo.WxUserInfoModel;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zwr on 2016/6/28 0028.
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /***
     * 根据手机号或OpenId获取用户信息(qiliping:2016-3-29)
     * @param word
     * @return
     */
    @Override
    public User getUserByPhoneOrOpenId(String word) {
        UserExample example = new UserExample();
        example.or().andUserPhoneEqualTo(word);
        example.or().andWxOpenidEqualTo(word);
        List<User> lists = userMapper.selectByExample(example);
        if(lists==null||lists.size()==0) return null;
        else return lists.get(0);
    }

    /***
     * 根据OpenId获取用户信息-微信登录用(qiliping)
     * @param openId
     * @return
     */
    @Override
    public User getUserByOpenId(String openId) {
        UserExample example = new UserExample();
        example.or().andWxOpenidEqualTo(openId);
        List<User> lists = userMapper.selectByExample(example);
        if(lists==null||lists.size()==0) return null;
        else return lists.get(0);
    }

    /***
     * 新增微信用户(qiliping)
     * @param token
     * @param wxUser
     * @return
     */
    @Override
    public int insertWxUser(WxAccessTokenModel token, WxUserInfoModel wxUser) {
        User user = new User();
        user.setUserId(UUID.randomUUID().toString());
        user.setUserName(wxUser.getNickname());
        user.setRegTime(new Date());
        user.setUserHead(wxUser.getHeadimgurl());
//        user.setGender(wxUser.getSex());
        user.setAccountBalance(BigDecimal.valueOf(0.0));
        user.setIntegralBalance(0);
        user.setCommissionBalance(BigDecimal.valueOf(0.0));
        user.setRegSource("WX");

        user.setAddrCountry(wxUser.getCountry());
        user.setAddrProvinceName(wxUser.getProvince());
        user.setAddrCityName(wxUser.getCity());
        user.setWxOpenid(token.getOpenid());
        user.setWxUnionid(wxUser.getUnionid());
        user.setWxAccessToken(token.getAccess_token());
        user.setWxRefreshToken(token.getRefresh_token());
        user.setWxTokenBegin(new Date());
        user.setWxTokenExpiresIn(token.getExpires_in());
        user.setUserTypeTypeId(0);
        return userMapper.insertSelective(user);
    }

    /***
     * 更新微信用户资料(qiliping)
     * @param userId
     * @param token
     * @param wxUser
     * @return
     */
    @Override
    public int updateWxUser(String userId, WxAccessTokenModel token, WxUserInfoModel wxUser) {
        User user = new User();
        user.setUserId(userId);
        user.setUserName(wxUser.getNickname());
        user.setUserHead(wxUser.getHeadimgurl());
        user.setAddrCountry(wxUser.getCountry());
        user.setAddrProvinceName(wxUser.getProvince());
        user.setAddrCityName(wxUser.getCity());
//        user.setGender(wxUser.getSex());
        user.setWxUnionid(wxUser.getUnionid());
        user.setWxAccessToken(token.getAccess_token());
        user.setWxTokenBegin(new Date());
        user.setWxUnionid(wxUser.getUnionid());
        user.setWxRefreshToken(token.getRefresh_token());
        user.setWxTokenExpiresIn(token.getExpires_in());
        return userMapper.updateByPrimaryKeySelective(user);
    }


    @Override
    public User getUserByPhone(String phone) {
        UserExample example = new UserExample();
        example.or().andUserPhoneEqualTo(phone);
//		System.out.println(example.getOredCriteria().toString());
        List<User> list = userMapper.selectByExample(example);
//		System.out.println(list);
        if (list == null || list.size() == 0) {
            return null;
        } else {
            //System.out.println(list.size());
            return list.get(0);
        }
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updatePassword(String userId, String password) {
        User user = new User();
        user.setUserPwd(password);
        user.setUserId(userId);

        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User getUserByUserId(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updatePassword(String userId, String userPwd, String userPhone) {
        User user=new User();
        user.setUserId(userId);
        user.setUserPwd(userPwd);
        user.setUserPhone(userPhone);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int updatePhone(String userId, String userPhone) {
        User user=new User();
        user.setUserId(userId);
        user.setUserPhone(userPhone);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User getUserByPhoneByUserId(String phone, String userId) {
        UserExample example = new UserExample();
        example.or().andUserPhoneEqualTo(phone);
        example.or().andUserIdEqualTo(userId);
        List<User> lists = userMapper.selectByExample(example);
        if (lists!=null&&lists.size()>0){
            return lists.get(0);
        } else{
            return null;
        }
    }

    @Override
    public int updateName(String userId, String userName) {
        User user=new User();
        user.setUserId(userId);
        user.setUserName(userName);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public PageList<User> setUserList(Integer pageIndex, Integer pageSize, String userName, Date start, Date edate) {
        PageHelper.startPage(pageIndex,pageSize);
        User user=new User();
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isEmpty(userName) == false) {
            criteria.andUserNameLike(userName);
        }
        if (start!=null && edate!=null){
            criteria.andRegTimeBetween(start,edate);
        }
        if(start != null&& edate==null ) {
            criteria.andRegTimeGreaterThanOrEqualTo(start);
        }
        if (edate!=null && start==null){
            criteria.andRegTimeLessThan(start);
        }
        example.or(criteria);
        example.setOrderByClause("user_id desc");
        List<User>list=userMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        PageList pageList = new PageList<User>();
        pageList.list = pageInfo.getList();
        pageList.totalCount = pageInfo.getTotal();
        pageList.totalPage=pageInfo.getPages();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        return pageList;
    }
    @Override
    public int updateAccountBalance(String userId, BigDecimal accountBalance) {
        User user=new User();
        user.setUserId(userId);
        user.setAccountBalance(accountBalance);
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
